using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 召唤Buff - 召唤辅助单位
/// </summary>
public class SummonBuff : BuffBase
{
    private GameObject m_SummonedUnit;
    private Entity m_SummonedEntity;
    private float m_SummonDuration;
    private bool m_IsSummoned;

    public SummonBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        if (buffData is SummonData summonData)
        {
            m_SummonDuration = summonData.SummonDuration;
        }
        else
        {
            m_SummonDuration = 10.0f; // 默认召唤持续时间
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 执行召唤
        ExecuteSummon();
        
        // 播放召唤特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log($"{Target.name} 召唤了辅助单���");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        
        // 检查召唤单位是否还存在
        if (m_IsSummoned && m_SummonedEntity != null)
        {
            // 可以在这里添加召唤单位的AI逻辑
            // 比如跟随施法者、攻击敌人等
        }
    }

    private void ExecuteSummon()
    {
        if (m_IsSummoned) return;
        
        m_IsSummoned = true;
        
        // 获取召唤单位预制���
        GameObject summonPrefab = GetSummonPrefab();
        if (summonPrefab != null)
        {
            // 计算召唤位置（施法者附近）
            Vector3 summonPosition = Caster != null ?
                Caster.transform.position + Vector3.right * 2f : 
                Target.transform.position + Vector3.right * 2f;
            
            // 实例化召唤单���
            m_SummonedUnit = GameObject.Instantiate(summonPrefab, summonPosition, Quaternion.identity);
            m_SummonedEntity = m_SummonedUnit.GetComponent<Entity>();
            
            // 设置召唤单位的属���
            if (m_SummonedEntity != null)
            {
                // 设置召唤单位为施法者的盟友
                // m_SummonedEntity.SetAlly(Caster);
                
                // 设置召唤单位的持续时���
                var healthComponent = m_SummonedEntity.GetComponent<HealthComponent>();
                if (healthComponent != null)
                {
                    // 可以设置召唤单位的生命值等属���
                }
            }
            
            // 设置动画状���
            if (Target.Animator != null)
            {
                Target.Animator.SetTrigger("summon");
            }
        }
    }

    private GameObject GetSummonPrefab()
    {
        // 这里需要根据具体的召唤数据来获取预制体
        // 可以从BuffData中获取召唤单位的信息
        if (BuffData is SummonData summonData)
        {
            return summonData.SummonPrefab;
        }
        
        // 返回默认召唤单位（如果有的话���
        return null;
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("SummonBuff", $"{Target.name} 召唤效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 清理召唤单位
        if (m_SummonedUnit != null)
        {
            GameObject.Destroy(m_SummonedUnit);
            m_SummonedUnit = null;
            m_SummonedEntity = null;
        }
        
        m_IsSummoned = false;
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Summon"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return false; // 召唤效果不能叠加
    }
}
